<? include ("../init.php") ?>
<?
  $cat = $_POST['cat'];
  $collection = $_POST['collection'];
  $name = addslashes($_POST['name']);

  $style_number = addslashes(strtolower($_POST['style_number']));
  $product_type = addslashes(strtolower($_POST['product_type']));
  $metal = addslashes($_POST['metal']);
  $lenght_inch = addslashes($_POST['lenght_inch']);
  $lenght_mm = addslashes($_POST['lenght_mm']);
  $width_mm = addslashes($_POST['width_mm']);

  $detail = addslashes($_POST['detail']);
  $active = ($_POST['active']);
  $user_allow = ($_POST['user_allow']);
  
  $allow = 3;
  foreach($user_allow as $v){
    $allow -= $v;
  }
  

  $sql = "INSERT INTO hs_product(c_id,pc_id,p_name,p_detail,p_active,user_allow,p_style_number,p_type,p_metal,p_lenght_inch,p_lenght_mm,p_width_mm) VALUES('$cat','$collection','$name','$detail','$active','$allow','$style_number','$product_type','$metal','$lenght_inch','$lenght_mm','$width_mm')";

  $db->execute($sql);
  
  $aid = $db->lastInsertedId();
  
  // ---------- UPLOADS Image ----------
  $handle = new Upload($_FILES['image']);
  
  if($handle->file_src_name != '')
  {
    if ($handle->uploaded)
    {
        $handle->image_resize = true;
        $handle->image_ratio_fill = true;
        $handle->image_x = 600;
        $handle->image_y = 600;
        $handle->Process(APPPATH.'/images/products/product/'.$aid);
        
        if ($handle->processed)
        {
           $sql = "UPDATE hs_product SET p_img='$handle->file_dst_name' WHERE p_id=$aid";
           if(!mysql_query($sql))
           {
                $data_add[1] = "Can\'t upload image file, please check again or contact the webmaster.";
                unlink(APPPATH."/images/products/product/$aid/".$handle->file_dst_name);
           }
        }
        else
        {
            $data_add[1] = "Can\'t upload image file, please check again or contact the webmaster.";
        }
        
        $handle->clean();
    }
    else
    {
        $data_add[1] = "Can\'t upload image file, please check again or contact the webmaster.";
    }
  }
  
  //$sql = "UPDATE product SET product_img='$name' WHERE aid=$aid";
  //$db->execute($sql);
  
  header("Location:products.php");
?>